package org.jeecg.modules.demo.hd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.modules.demo.hd.entity.AppUserGoldMain;
import org.jeecg.modules.demo.hd.entity.AppUserShareMain;
import org.jeecg.modules.demo.hd.mapper.AppUserShareMainMapper;
import org.jeecg.modules.demo.hd.service.IAppUserShareMainService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: app_user_share_main
 * @Author: jeecg-boot
 * @Date: 2025-04-30
 * @Version: V1.0
 */
@Service
public class AppUserShareMainServiceImpl extends ServiceImpl<AppUserShareMainMapper, AppUserShareMain> implements IAppUserShareMainService {


    @Override
    @Transactional
    public boolean updateShareMain(String userId, Integer shareNum) {
        // 获取用户金币主表实体
        LambdaQueryWrapper<AppUserShareMain> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AppUserShareMain::getUserId, userId);
        AppUserShareMain appUserShareMain = this.baseMapper.selectOne(queryWrapper);
        if (appUserShareMain == null) {
            appUserShareMain = new AppUserShareMain();
            appUserShareMain.setUserId(userId);
            appUserShareMain.setShareNum((double) shareNum);
            appUserShareMain.setStatus("1");
            this.baseMapper.insert(appUserShareMain);
        } else {
            Double goldNum1 = appUserShareMain.getShareNum();
            appUserShareMain.setShareNum(goldNum1 + shareNum);
            this.baseMapper.updateById(appUserShareMain);
        }
        return true;
    }

    @Override
    public Double queryCurrentShare(String userId) {
        LambdaQueryWrapper<AppUserShareMain> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AppUserShareMain::getUserId, userId);
        AppUserShareMain model = this.baseMapper.selectOne(queryWrapper);
        if (model != null) {
            return model.getShareNum();
        }
        return 0d;
    }
}
